package com.donquijote.spring6.service.impl;

import com.donquijote.spring6.dao.ActDao;
import com.donquijote.spring6.service.ActService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author 421527548@qq.com
 * @version V1.0
 * @title
 * @description
 * @date 2022-12-29 12:06
 */
@Service("actServiceImpl")

public class ActServiceImpl implements ActService {

    @Resource(name = "actDaoImpl")
    private ActDao actDao;

    @Transactional
    @Override
    public int transfer(String fromAct, String toAct, Double money) {
        Double fromBalance = actDao.selectBalance(fromAct);
        if (fromBalance < money) {
            throw new RuntimeException("余额不足");
        }
        //余额充足
        //两个对象中的余额修改
        int count = actDao.updateBalance(fromAct, -money);

        String s = null;
        s.toString();

        count += actDao.updateBalance(toAct,money);

        //如果count结果不等于2，则转账失败
        if (count != 2) {
            throw new RuntimeException("转账失败");
        }
        return 1;
    }
}
